package com.leen.aspect.permiss;


import com.leen.aspect.annon.RequiredPermission;
import com.leen.util.ThreadLocalUser;
import org.apache.commons.lang3.StringUtils;

import java.util.Set;

/**
 * 角色或权限其中一个满足
 * @author wy
 */
public class AuthorizationOr implements Authorization {
    private RequiredPermission requiredPermission;

    public AuthorizationOr(RequiredPermission requiredPermission) {
        this.requiredPermission = requiredPermission;
    }

    @Override
    public boolean checkPermission() {

        String role=this.requiredPermission.roleValue();
        String[] permArr=this.requiredPermission.permissionValue();
        //拿到当前用户拥有的权限
        Set<String> ownerRoles= ThreadLocalUser.getInstance().getCurrentUser().getRoles();
        //要检查权限
        boolean rolePassed=true;
        if(StringUtils.isNotBlank(role)  ){
            if(ownerRoles==null ||  ownerRoles.size()==0){
                rolePassed=false;
            }else{
                //检查是否权限一致
                rolePassed=ownerRoles.contains(role);
                if(rolePassed){
                    return true;
                }
            }
        }

        //拿到当前用户拥有的菜单权限
        Set<String>  ownerPermissions= ThreadLocalUser.getInstance().getCurrentUser().getStringPermissions();
        // 要不要检查菜单权限
        boolean hasPerm=false;
        if(permArr!=null && permArr.length>0){
            if(ownerPermissions==null || ownerPermissions.size()==0){
                return false;
            }
            for(String s :permArr){
                hasPerm=ownerPermissions.contains(s);
                if(hasPerm){
                    break;
                }
            }
        }

        return hasPerm;

    }
}
